package com.example.download

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import android.widget.ViewSwitcher
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.example.download.databinding.ActivityHelpBinding
import com.example.download.fragment.PageFragment1
import com.example.download.fragment.PageFragment2
import com.example.download.fragment.PageFragment3
import com.example.download.fragment.PageFragment4


class Help : AppCompatActivity() {
    private lateinit var activityHelpBinding: ActivityHelpBinding
    private lateinit var helpViewPager2: ViewPager2
    private lateinit var iv_help_close: ImageView
    private lateinit var iv_help_back: ImageView
    private lateinit var tv_help_next1: TextView
    private lateinit var tv_help_next2: TextView
    private lateinit var vs_help_switch:ViewSwitcher
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        activityHelpBinding = ActivityHelpBinding.inflate(layoutInflater)
        setContentView(activityHelpBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        helpViewPager2 = activityHelpBinding.helpViewpager2
        helpViewPager2.adapter = object : FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return 4
            }

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 -> PageFragment1()
                    1 -> PageFragment2()
                    2 -> PageFragment3()
                    3 -> PageFragment4()
                    else -> PageFragment1()
                }
            }
        }
        iv_help_close = activityHelpBinding.ivHelpClose
        iv_help_close.setOnClickListener {
            finish()
        }

        iv_help_back = activityHelpBinding.ivHelpBack
        iv_help_back.setOnClickListener {
            lastPage()
        }

        tv_help_next1 = activityHelpBinding.tvHelpNext1
        tv_help_next1.setOnClickListener {
            nextPage()
        }

        tv_help_next2 = activityHelpBinding.tvHelpNext2
        tv_help_next2.setOnClickListener {
            if (helpViewPager2.currentItem == 3) {
                finish()
            } else {
                nextPage()
            }
        }


        vs_help_switch = activityHelpBinding.vsHelpSwitch
        helpViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position == 0) {
                    vs_help_switch.displayedChild = 0
                } else if (position == 1) {
                    vs_help_switch.displayedChild = 1
                }
            }
        })

    }
    fun nextPage() {
        val nextItem = helpViewPager2.currentItem + 1
        if (nextItem <= 3) {
            helpViewPager2.setCurrentItem(nextItem, true)
        }
    }
    fun lastPage() {
        val lastItem = helpViewPager2.currentItem - 1
        if (lastItem >= 0) {
            helpViewPager2.setCurrentItem(lastItem, true)
        }
    }
}